package com.htudata.core.util.operation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpOperationUtils {

  
  public static String sendPost(String urlWithoutParam, String urlParam, String encoding) throws Exception{
    return getConnReturnDataForStr(
        getConnInputStream(sendPostReturnConn(urlWithoutParam, urlParam, encoding), encoding), encoding);
  }
  
  public static String sendGet(String url, String encoding) throws Exception {
    String urlWithoutParam = url;
    String urlParam = "";
    if (url.indexOf("?") != -1) {
      urlWithoutParam = UrlOperationUtils.getUrlWithoutParam(url);
      urlParam = UrlOperationUtils.getUrlParamFromUrl(url);
    } 
    return sendGet(urlWithoutParam, urlParam, encoding);
  }
  
  public static String sendGet(String urlWithoutParam, String urlParam, String encoding) throws Exception{
    return getConnReturnDataForStr(
        getConnInputStream(sendGetReturnConn(urlWithoutParam, urlParam, encoding), encoding), encoding);
  }

  public static String getConnReturnDataForStr(InputStream inputStream, String encoding) throws Exception {
    if (inputStream == null)
      return ""; 
    BufferedReader in = null;
    try {
      in = new BufferedReader(new InputStreamReader(inputStream, encoding));
      String line = "";
      StringBuffer result = new StringBuffer();
      while ((line = in.readLine()) != null)
        result.append(line); 
      return result.toString();
    } catch (Exception e) {
      throw e;
    }
    finally {
      try {
        if (in != null) {
          in.close();
          in = null;
        } 
      } catch (IOException e) {

      } 
    }
  }
  
  public static InputStream getConnInputStream(URLConnection conn, String encoding)throws Exception {
    if (conn == null)
      return null; 
    try {
      InputStream result = conn.getInputStream();
      return result;
    } catch (IOException e) {
       throw e;
    } 
  }
  
  public static URLConnection sendPostReturnConn(String urlWithoutParam, String urlParam, String encoding) throws Exception{
    URLConnection result = null;
    try {
      URL urlTemp = new URL(urlWithoutParam);
      result = urlTemp.openConnection();
    }
    catch (Exception e) {
       throw e;
    } 
    result.setRequestProperty("accept", "*/*");
    result.setRequestProperty("connection", "Keep-Alive");
    result.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    result.setReadTimeout(10000);
    result.setConnectTimeout(10000);
    result.setDoOutput(true);
    result.setDoInput(true);
    OutputStreamWriter out = null;
    try {
      out = new OutputStreamWriter(result.getOutputStream(), encoding);
      out.write(urlParam);
      out.flush();
      return result;
    } catch (Exception e) {
      throw e;
    } finally {
      try {
        if (out != null) {
          out.close();
          out = null;
        } 
      } catch (IOException e) {

      } 
    }
  }
  
  public static URLConnection sendGetReturnConn(String urlWithoutParam, String urlParam, String encoding)throws Exception {
    URLConnection result = null;
    try {
      URL urlTemp = new URL(UrlOperationUtils.getUrlWithParam(urlWithoutParam, urlParam));
      result = urlTemp.openConnection();
    } catch (Exception e) {
       throw e;
    } 
    result.setRequestProperty("accept", "*/*");
    result.setRequestProperty("connection", "Keep-Alive");
    result.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    result.setReadTimeout(10000);
    result.setConnectTimeout(10000);
    try {
      result.connect();
      return result;
    } catch (IOException e) {
       throw e;
    } 
  }
}
